<template>
  <el-dialog
    title="提示"
    :visible.sync="dialogVisible"
    width="600px"
    class="register"
    :show-close="false"
  >
    <div slot="title" class="title">用户注册</div>
    <el-form label-width="100px" :model="form" :rules="rules" ref="form">
      <el-form-item label="头像" prop="avatar">
        <el-upload
          class="avatar-uploader"
          :action="baseUrl +'/uploads'"
          name="image"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.avatar" :src="baseUrl+'/'+form.avatar" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="昵称" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input show-password v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="图形码" prop="code">
        <el-row>
          <el-col :span="15">
            <el-input v-model="form.code"></el-input>
          </el-col>
          <el-col :span="8" :offset="1">
            <img :src="baseUrl + '/captcha?type=sendsms'" alt class="img-code" />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="验证码" prop="rcode">
        <el-row>
          <el-col :span="15">
            <el-input v-model="form.rcode"></el-input>
          </el-col>
          <el-col :span="8" :offset="1">
            <el-button class="btn">获取用户验证码</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer footer">
      <el-button>取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
        form:{
            username:'',
            phone:'',
            email:'',
            avatar:'',
            password:'',
            code:'',
            rcode:''
        },
        rules:{
            username:[{required:true,message:"请输入用户名",trigger:'change'},
              
            ],
            phone:[{required:true,message:"请输入手机号",trigger:'change'}],
            email:[{required:true,message:"请输入邮箱号",trigger:'change'},
             { validator:(rule,value,callback)=>{
                        let _reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
                        if(_reg.test(value)){
                            callback()
                        }else{
                            callback(new Error('请输入正确的邮箱号'))
                        }
                }}],
            avatar:[{required:true,message:"请上传头像",trigger:'change'}],
            password:[{required:true,message:"请输入密码",trigger:'change'}],
            code:[{required:true,message:"请输入验证码",trigger:'change'}],
            rcode:[{required:true,message:"请输入手机验证码",trigger:'change'}],
        },
      baseUrl: process.env.VUE_APP_URL,
      dialogVisible: false,
      imageUrl: "",
    };
  },
  methods: {
      submit(){
          this.$refs.form.validate(res=>{
              if(res){
                  this.$message.success('验证成功')
              }else{
                  this.$message.error('验证失败')
              }
          })
      },
    handleAvatarSuccess(res , file ) {
     /*  this.imageUrl = URL.createObjectURL(file.raw); */
      this.form.avatar = res.data.file_path
      console.log('res',res);
      console.log('file',file);
      this.$refs.form.validateField(['avatar'])
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" ||file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less">
.register {
  .title {
    height: 53px;
    background: linear-gradient(to right, #02c3fa, #1296fa);
    text-align: center;
    color: #fff;
    line-height: 53px;
  }
  .el-dialog__header {
    padding: 0;
  }
  .img-code {
    width: 100%;
    height: 40px;
  }
  .btn {
    width: 100%;
  }
  .footer {
    text-align: center;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .avatar-uploader {
    text-align: center;
  }
}
</style>